import { createSSRApp } from 'vue'
import App from './App.vue'
import pinia from '@/store/index'

import i18n from './i18n.js'
import { isDevelopment } from './utils/utils'
import { useUserInfo } from '@/store/user'

const handleBeforeInstallPrompt = (e) => {
  e.preventDefault()
  console.log('🔥 beforeinstallprompt fired')
  useUserInfo().setDeferredPrompt(e)
}

window.removeEventListener('beforeinstallprompt', handleBeforeInstallPrompt)

window.addEventListener('beforeinstallprompt', handleBeforeInstallPrompt)

if ('serviceWorker' in navigator) {
  window.addEventListener('load', () => {
    const urlPree = isDevelopment()
    navigator.serviceWorker
      .register(urlPree + '/static/pwa/sw.js')
      .then((registration) => {
        registration.update()
        console.log('SW registered:', registration)
      })
      .catch((error) => {
        console.log('SW registration failed:', error)
      })
  })
}

export function createApp() {
  const app = createSSRApp(App)

  
  uni.getSystemInfo({
    success: function (e) {
      
      app.config.globalProperties.$StatusBar = e.statusBarHeight
      if (e.platform === 'android') {
        app.config.globalProperties.$CustomBar = e.statusBarHeight + 50
      } else {
        app.config.globalProperties.$CustomBar = e.statusBarHeight + 45
      }
      

      
      app.config.globalProperties.$StatusBar = e.statusBarHeight
      const custom = wx.getMenuButtonBoundingClientRect()
      app.config.globalProperties.$Custom = custom
      app.config.globalProperties.$CustomBar = custom.bottom + custom.top - e.statusBarHeight
      

      
      app.config.globalProperties.$windowHeight = e.windowHeight
      
      app.config.globalProperties.$navHeight = e.statusBarHeight * (750 / e.windowWidth) + 91
      app.config.globalProperties.$SystemInfo = e
    },
  })

  app.use(pinia)

  
  

  app.use(i18n)
  
  return {
    app,
  }
}
